﻿(function () {
    appModule.controller('tenant.views.manuscript.createOrEditManuscript', [
        '$scope', '$uibModalInstance', 'abp.services.app.manuscript', 'manuscriptId', 'parameterTypes', 
        function ($scope, $uibModalInstance, manuscriptService, manuscriptId, parameterTypes) {
            var vm = this;

            vm.saving = false;
            //编辑框输出转码
            vm.htmlDecodeByRegExp = function (str) {
                var s = "";
                if (!str) return "";
                if (str.length == 0) return "";
                s = str.replace(/&amp;/g, "&");
                s = s.replace(/&lt;/g, "<");
                s = s.replace(/&gt;/g, ">");
                s = s.replace(/&nbsp;/g, " ");
                s = s.replace(/&#39;/g, "\'");
                s = s.replace(/&quot;/g, "\"");
                return s;
            }
            //编辑器工具
            vm.editor = {
                tools: [
                    "bold", "italic", "underline", "strikethrough", "justifyLeft",
                    "justifyCenter", "justifyRight", "justifyFull", "insertUnorderedList",
                    "insertOrderedList", "indent", "outdent", "createLink", "unlink",
                    "insertImage", "insertFile", "subscript", "superscript", "createTable",
                    "addRowAbove", "addRowBelow", "addColumnLeft", "addColumnRight", "deleteRow",
                    "deleteColumn", "viewHtml", "formatting",
                    "cleanFormatting", "fontName", "fontSize", "foreColor", "backColor", "print"
                ]
            }

            vm.save = function () {
                vm.manuscript.manuscriptStatusID = 1;
                manuscriptService.createOrUpdateManuscript({ manuscript: vm.manuscript }).success(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });
            };

            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };
            //筛选类型
            vm.filtrationParameterTypes = function (data, code) {
                var newData = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].code == code) {
                        newData.push(data[i]);
                    }
                }
                return newData
            };
            function init() {
                if (manuscriptId) {
                    manuscriptService.getManuscriptForEdit({ id: manuscriptId }).success(function (result) {
                        vm.manuscript = result.manuscript;
                        if (!result.manuscript) {

                        } else {
                            vm.manuscript.manuscriptContent = vm.htmlDecodeByRegExp(vm.manuscript.manuscriptContent);
                        }
                    });
                } else {
                    vm.manuscript = {};
                    vm.manuscript.manuscriptContent = "在此填入稿件内容···";
                };
                //获取稿件类别 code=1
                vm.manuscriptType = vm.filtrationParameterTypes(parameterTypes, 1);
                //获取栏目类别 code=2
                vm.columnType = vm.filtrationParameterTypes(parameterTypes, 2);
                //添加时默认选项
                if (!manuscriptId) {
                    vm.manuscript.manuscriptTypeID = vm.manuscriptType[0].id;
                    vm.manuscript.columnTypeID = vm.columnType[0].id;
                }
                
            }
            init();

        }
    ]);
})();
